home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / educate / wordy442.zip / PATTERN.C < prev    next >
C/C++ Source or Header  |  1996-06-20  |  9KB  |  343 lines

  1. /**************************************************************************/
  2. /*                            PATTERN-MATCH UTILITY                       */
  3. /*                                                                        */
  4. /*                                                                        */
  5. /*                                     M\Cooper                           */
  6. /*                                    PO Box 237                          */
  7. /*                            St. David, AZ 85630-0237                    */
  8. /*                        -------------------------------                 */
  9. /*                        Email:  thegrendel@theriver.com                 */
  10. /*                                                                        */
  11. /*                $2.00 to register the entire WORDY package                 */
  12. /*                                                                        */
  13. /**************************************************************************/
  14.  
  15. #include <conio.h>
  16. #include <ctype.h>
  17. #include "srch.h"
  18. //srch.h (source) is part of the WORDY package
  19.  
  20.  
  21. #define FILE_OPENING_ERROR 3
  22. #define FILENAME_MAXLEN 40
  23. #define CR "\n"
  24. #define FILE_SUFFIX ".pat"
  25. #define MAXLEN 30
  26. #define MAXWORDLEN 30
  27. #define LINE_LEN 80
  28. #define NOARGS 1
  29. #define INCREMENT 1
  30. #define SPACE ' '
  31. #define DBLBAR 205
  32. #define WILDCARD '?'
  33. //Wildcard character in command line, may be changed
  34. #define WRONG_SPECS 10
  35. #define PATTERN_TOO_LONG 20
  36.  
  37. #define BUFFERSIZE 8192
  38.  
  39. char ad[] =
  40. "PATTERN MATCH util. : M\\Cooper, PO Box 237, St. David, AZ 85630-0237";
  41.  
  42. typedef enum { FALSE, TRUE } Boolean;
  43. typedef enum { OFF, ON } Flag;
  44.  
  45. Boolean qualify( char *In );
  46. void getword( char *lset, char *filename );
  47. void center( char *strng );
  48. Boolean is_valid_word( char *Pattern, char *Word );
  49.  
  50. void main( int argc, char **argv )
  51. {
  52.  
  53.    char letterset [MAXLEN],
  54.         iset [5 * MAXLEN],
  55.         filename [FILENAME_MAXLEN];
  56.  
  57.      if( argc == NOARGS )
  58.         {
  59.         clrscr();
  60.         puts( "Enter a LETTER PATTERN to test [example 1231234] " );
  61.         gets( iset );
  62.      if( strlen( iset ) >= MAXWORDLEN - 2 )
  63.         {
  64.         printf( "\n\nPattern too long!\n" );
  65.         exit( PATTERN_TOO_LONG );
  66.         }
  67.      strcpy( letterset, iset );
  68.      strcpy( filename, "word.lst" );  //default
  69.         }
  70.      else
  71.      if( argc == NOARGS + 1 )
  72.        {
  73.        if( strlen( *(argv +1) ) >= MAXWORDLEN - 2 )
  74.           {
  75.           printf( "\n\nPattern too long!\n" );
  76.           exit( PATTERN_TOO_LONG);
  77.           }
  78.           strcpy( letterset, *(argv + 1) );
  79.        strcpy( filename, "word.lst" ); //default
  80.        }
  81.    else
  82.       {      
  83.        if( strlen( *(argv +1) ) >= MAXWORDLEN - 2 )
  84.           {
  85.           printf( "\n\nPattern too long!\n" );
  86.           exit( PATTERN_TOO_LONG);
  87.           }
  88.       strcpy( letterset, *(argv + 1) );
  89.       strcpy( filename, *(argv + 2) );
  90.       }
  91.  
  92.       if( !qualify( letterset ) )
  93.          {
  94.          printf( "\nInput to program must be a string of digits\n" );
  95.          printf( "in *ascending* order, beginning with '1'.\n" );
  96.          printf( "Previously used digits may be repeated.\n" );
  97.          exit( WRONG_SPECS );
  98.          }
  99.  
  100.      getword( letterset, filename );
  101. }
  102.  
  103.  
  104. /**********************************WORDTEST********************************/
  105. /*       Function tests if word is constructible from Letterset           */
  106. /*                 Args in: char *letterset, char *word                   */
  107. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  108. /**************************************************************************/
  109.  
  110. Boolean wordtest( char *letterset, char *word )
  111. {
  112.     Boolean error_flag;
  113.     static char dup_lset[ MAXLEN ];
  114.     register unsigned int cnt = 0,
  115.                       u,
  116.                       v;
  117.  
  118.      strcpy( dup_lset, letterset );
  119.          
  120.      u = strlen( word );
  121.       v = strlen( letterset );
  122.  
  123.      while( ( *letterset == *word ) || *letterset == WILDCARD )
  124.         {
  125.         letterset++;
  126.         word++;
  127.         cnt++;
  128.         }
  129.  
  130.      if( u <= cnt && u == v )
  131.         error_flag = TRUE;
  132.      else
  133.         error_flag = FALSE;
  134.  
  135.  
  136.         return( error_flag );
  137. }
  138.  
  139. /**************************************************************************/
  140.  
  141. void getword( char *letter_set, char *filename )
  142. {
  143.  
  144.     char    l_set [ MAXLEN ],
  145.         word [ MAXLEN ],
  146.         tempstr [ MAXLEN + 1 ],
  147.         targetfile [ MAXLEN ],
  148.         bar [ LINE_LEN + 1 ],
  149.         double_bar [ LINE_LEN + 1 ],
  150.         messg [7];
  151.  
  152.     FILE *fptr,
  153.         *tfile;
  154.     int fnamelen;
  155.     long wcount = 0L;
  156.  
  157.        memset( bar, '*', LINE_LEN );
  158.        *( bar + LINE_LEN ) = NULL;
  159.        memset( double_bar, DBLBAR, LINE_LEN );
  160.        *( double_bar + LINE_LEN ) = NULL;
  161.  
  162.        /*************opening credits*************/
  163.        clrscr();
  164.        printf( double_bar );
  165.        strcpy( tempstr, ad );
  166.        center ( tempstr );
  167.        printf( tempstr );
  168.        printf( CR );
  169.        printf( double_bar );
  170.        printf( CR );
  171.        /****************************************/
  172.  
  173.  
  174.        strcpy ( l_set, letter_set );
  175.        strcat ( letter_set, CR );
  176.  
  177.        /*   Create name of file to store derived words in   */
  178.        /*********************************************************/
  179.        strcpy( targetfile, "matched.pat" );
  180.        /*********************************************************/
  181.  
  182.        if( !( fptr = fopen( filename, "rt" ) ) )
  183.          {
  184.          printf( "\7\7\7Cannot open word file %s!", filename );
  185.          exit( FILE_OPENING_ERROR );
  186.          }
  187.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  188.          exit( FILE_OPENING_ERROR + 1 );
  189.  
  190.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  191.          {
  192.          printf( "\7\7\7Cannot open file to save words in!" );
  193.          exit ( FILE_OPENING_ERROR + 1 );
  194.          }
  195.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  196.          exit( FILE_OPENING_ERROR + 2 );
  197.  
  198.        /**************'Wait' Message************/
  199.        printf( CR CR );
  200.        printf( "WORKING...\n\n" );
  201.        printf( "This will take a few seconds...\n" );
  202.        printf( "Please be patient.\n\n" );
  203.        printf( "Now searching 100,000+ word file and writing file of valid words.\n\n" );
  204.        /*****************************************/
  205.  
  206.  
  207.  
  208.  
  209.  
  210.        sprintf( tempstr, "Words fitting the pattern of: %s\n", strupr( l_set ) );
  211.        center( tempstr );
  212.        fprintf( tfile, double_bar );
  213.       fprintf( tfile, CR );
  214.        fprintf( tfile, tempstr );
  215.        fprintf( tfile, double_bar );
  216.        fprintf( tfile, CR );
  217.  
  218.  
  219.          /*********************Main Loop*************/     
  220.           while( fgets( word, MAXLEN, fptr ) != NULL )
  221.  
  222.             if( is_valid_word( letter_set, word ) )
  223.                {
  224.                fprintf( tfile, "%s", word );
  225.                wcount++;
  226.                }
  227.           /*******************************************/
  228.  
  229.           if( wcount == INCREMENT )
  230.             strcpy( messg, "word" );
  231.           else
  232.             strcpy( messg, "words" );
  233.  
  234.           fprintf( tfile, bar );
  235.       fprintf( tfile, CR );
  236.           sprintf( tempstr, "%ld %s fit the pattern of %s.",
  237.                  wcount, messg, l_set );
  238.           center( tempstr );              
  239.           fprintf( tfile, tempstr );
  240.           fprintf( tfile, "\n\n" );
  241.  
  242.           center( ad );
  243.           fprintf( tfile, ad );
  244.  
  245.           fcloseall();
  246.  
  247.           sprintf( tempstr,
  248.                  "The file %s has %ld %s fitting the pattern of %s\7.",
  249.                  targetfile, wcount, messg, l_set );
  250.           center( tempstr );
  251.           printf( CR CR );
  252.           printf( tempstr );
  253.  
  254. }
  255.  
  256.  
  257.  
  258. void center( char *str )
  259. {
  260.    int padding;
  261.    char st [ LINE_LEN + INCREMENT ];
  262.  
  263.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  264.      memset( st, SPACE, padding );
  265.      *( st + padding ) = NULL;  //Terminate string
  266.      strcat( st, str );
  267.      strcpy( str, st );
  268.  
  269.      return;
  270. }
  271.  
  272.  
  273. Boolean is_valid_word( char *pattern, char *word )
  274. {
  275.    char letter,
  276.         wd [MAXWORDLEN],
  277.         *index;
  278.    int wlen,
  279.        n,
  280.        cindex = 0;
  281.  
  282.       strcpy( wd, word );  //wd is working copy of word.
  283.       wlen = strlen( wd );
  284.  
  285.       for( n = 0; n < wlen; n++ )
  286.          {
  287.          letter = *( wd + n );
  288.          if( !isalpha( letter ) )
  289.             continue;
  290.          cindex++;
  291.          
  292.          while( NULL != ( index = strchr( wd, letter) ) )
  293.               *index = cindex + '0';
  294.          //Replace all instances of that letter with appropriate 'number'.
  295.          }
  296.  
  297.